#!usr/bin/env python3
# -*- coding:utf-8 -*-
import os,random

root = os.path.abspath(os.curdir)

def joinPath(path):
	return os.path.join(root,path)

a = "00"#input("请输入:")
f = open(joinPath('log.txt'),'w') 	
print('把我在命令行输入的输出到txt:'+a,file=f) #print到文件
print('hello') #print到控制台

#类属性与实例属性的讨论
print("----------类属性与实例属性的讨论----------")

class AAA(object):
	val = 10;	
	def __init__(self):
		self.name = 'zhou'
		self._age = 20
		self.__score = 50 #私有变量
	
obj1 = AAA()
obj2 = AAA()

print("11 AAA.val=",AAA.val,'obj1.val=',obj1.val,'obj2.val=',obj2.val)

obj1.val += 2 #该操作是先查找AAA的val属性 然后得到该属性值赋值给obj1然后再用obj1的val值进行运算操作
#obj2.val = 12

print("22 AAA.val=",AAA.val,'obj1.val=',obj1.val,'obj2.val=',obj2.val)

AAA.val += 3

print("33 AAA.val=",AAA.val,'obj1.val=',obj1.val,'obj2.val=',obj2.val)


print(obj1.__dict__) #实例内的所有属性

print("1,2,3".split(','))

#冒泡排序:

scoreList = [2,5,1,7,3,8,10,30,80,20,40,12,34]
arrLen = len(scoreList);
for i in range(arrLen):
	for j in range(arrLen-1):
		if scoreList[j] > scoreList[j+1]:
			scoreList[j],scoreList[j+1] = scoreList[j+1],scoreList[j]
print('scoreList',scoreList)

scoreList = [
['a',98],
['c',45],
['b',70],
['d',85],
['h',85],
['f',92],
['g',30],
['e',65]
]
def onSortHandler(a1):	
	return a1[1];
	

scoreList.sort(key=lambda a:a[1],reverse=True) #降序

print('scoreList.sort=',scoreList)

random.shuffle(scoreList) #对list随机排序

print(scoreList)

'''
匿名函数: lambda
python 使用 lambda 来创建匿名函数。
lambda只是一个表达式，函数体比def简单很多。
lambda的主体是一个表达式，而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。
lambda函数拥有自己的命名空间，且不能访问自有参数列表之外或全局命名空间里的参数。
虽然lambda函数看起来只能写一行，却不等同于C或C++的内联函数，后者的目的是调用小函数时不占用栈内存从而增加运行效率

lambda [arg1 [,arg2,.....argn]]:expression

'''

import re
from datetime import datetime

dts = str(datetime.now())
dts = '_'.join(re.split(r"[\s]+",dts))
print("-----xx dts:",dts)

 
index={'合作':[(0, 5)], '每次':[(20, 99), (30, 11), (31, 69), (31, 80)],'在意':[(6, 40), (9, 5)]}
for key in index:
	print('----- ',key,' ------------')
	for i in range(len(index[key])):
		print(index[key][i])


lst = [50,60,70]
lstcp = lst[:] 
lstcp[1] = 100

print(" ----------- list dm =",lst,lstcp,(50 in lst))

#默认继承object可以省略
class Sprite:
	'''
		类说明测试:
		精灵类
	'''
	def __init__(self,name):
		self._name = name
	
	def traceName(self):
		print(self._name)

class SuperSprite(Sprite):
	pass

class MovieClip(SuperSprite):
	pass

mc = Sprite('master周')
mc.traceName()
#print(help(Sprite)) #打印类说明
print(mc.__doc__) #类注释
print(MovieClip.__base__)

mcs = [mc]
print((mc in mcs),mcs.index(mc))

num = -1.0125544
print(round(num,2))

#list循环键值的方法 
lst = [1,2,3,4,5]
for k,v in enumerate(lst): 
    print(k,v)
    
for k in range(len(lst)):
    print(lst[k])

#dict循环键值的方法
dtc = {"age":20,"name":"kfz","address":"gz"}
for k in dtc: #只迭代键
    print(k)
    
for v in dtc.values():#只迭代值
    print(v) 
for k,v in dtc.items():#迭代键和值
    print(k,v)


